﻿using JNPF.Basics.Logic;
using JNPF.Basics.Models;
using JNPF.Basics.Models.PlatForm.Dtos.TimeTask;
using JNPF.Utils;
using JNPF.Utils.Controller;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

namespace JNPF.Basics.Controller
{
    /// <summary>
    /// 定时任务
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2017-12-12
    /// </summary>
    [Route("api/PlatForm/[controller]")]
    public class TimeTaskController : BaseApiController
    {
        private TimeTaskBll timeTaskBll = new TimeTaskBll();

        #region GET
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResponseResult> GetList([FromQuery] PageInput input)
        {
            var data = await timeTaskBll.GetList(input);
            var output = new PageResult<TimeTaskListOutput>
            {
                list = data.list.MapToList<TimeTaskListOutput>(),
                pagination = data.pagination
            };
            return Success(output);
        }

        /// <summary>
        /// 列表（执行记录）
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <param name="id">任务Id</param>
        /// <returns></returns>
        [HttpGet("{id}/TaskLog")]
        public async Task<ResponseResult> GetTaskLogList([FromQuery] PageInput input, string id)
        {
            var data = await timeTaskBll.GetTaskLogList(input, id);
            var output = new PageResult<TimeTaskTaskLogListOutput>
            {
                list = data.list.MapToList<TimeTaskTaskLogListOutput>(),
                pagination = data.pagination
            };
            return Success(output);
        }

        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        [HttpGet("Info/{id}")]
        public async Task<ResponseResult> GetInfo(string id)
        {
            var data = await timeTaskBll.GetInfo(id);
            var output = data.MapTo<TimeTaskInfoOutput>();
            return Success(output);
        }
        #endregion

        #region POST
        /// <summary>
        /// 新建
        /// </summary>
        /// <param name="input">实体对象</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResponseResult> Create([FromBody] TimeTaskCrInput input)
        {
            if (await timeTaskBll.IsExistByEnCode(input.enCode, null))
            {
                return Error("编码已存在");
            }
            if (await timeTaskBll.IsExistByFullName(input.fullName, null))
            {
                return Error("名称已存在");
            }
            var entity = input.MapTo<TimeTaskEntity>();
            await timeTaskBll.Create(entity);
            return Success("新建成功");
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="id">主键值</param>
        /// <param name="input">实体对象</param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public async Task<ResponseResult> Update(string id, [FromBody] TimeTaskUpInput input)
        {
            if (await timeTaskBll.IsExistByEnCode(input.enCode, id))
            {
                return Error("编码已存在");
            }
            if (await timeTaskBll.IsExistByFullName(input.fullName, id))
            {
                return Error("名称已存在");
            }
            var entity = input.MapTo<TimeTaskEntity>();
            await timeTaskBll.Update(id, entity);
            return Success("更新成功");
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<ResponseResult> Delete(string id)
        {
            var entity = await timeTaskBll.GetInfo(id);
            if (entity != null)
            {
                await timeTaskBll.Delete(entity);
            }
            return Success("删除成功");
        }

        /// <summary>
        /// 停止
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        [HttpPut("{id}/Actions/Stop")]
        public async Task<ResponseResult> Stop(string id)
        {
            var entity = await timeTaskBll.GetInfo(id);
            if (entity != null)
            {
                entity.EnabledMark = 0;
                await timeTaskBll.Update(id, entity);
            }
            return Success("操作成功");
        }

        /// <summary>
        /// 启动
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        [HttpPut("{id}/Actions/Enable")]
        public async Task<ResponseResult> Enable(string id)
        {
            var entity = await timeTaskBll.GetInfo(id);
            if (entity != null)
            {
                entity.EnabledMark = 1;
                await timeTaskBll.Update(id, entity);
            }
            return Success("操作成功");
        }
        #endregion
    }
}
